/******************************************************************************
@file:  HttpConnection.java
@author Ramesh Anandhan
DESCRIPTION
-----------------------------------------------------------------------------
Copyright (c) 2011, GEEKWAVE SOLUTIONS INC.

All rights reserved.
 ******************************************************************************/
package com.smartc.connection;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;

public class HttpConnection {

	private final String TAG = "HttpConnection";

	private ArrayList<NameValuePair> params;

	private String mUrl;
	DefaultHandler mHandler;

	public HttpConnection() {
		params = new ArrayList<NameValuePair>();
	}

	public void AddParam(String name, String value) {
		params.add(new BasicNameValuePair(name, value));
	}

	public void clearParam() {
		params.clear();
	}

	public void setUrl(String url) {
		mUrl = url;
	}

	public void AddHandler(DefaultHandler handler) {
		mHandler = handler;
	}

	public void Execute() {

		Thread thread = new Thread() {
			@Override
			public void run() {
				HttpPost request = new HttpPost(mUrl);

				if (!params.isEmpty()) {
					try {
						request.setEntity(new UrlEncodedFormEntity(params,
								HTTP.UTF_8));
					} catch (UnsupportedEncodingException e) {
						e.printStackTrace();
					}
				}
				executeRequest(request, mUrl);
			}
		};
		thread.start();
	}

	private void executeRequest(HttpUriRequest request, String url) {
		HttpClient client = new DefaultHttpClient();

		HttpResponse httpResponse;

		try {
			httpResponse = client.execute(request);

			HttpEntity responseEntity = httpResponse.getEntity();

			SAXParserFactory spf = SAXParserFactory.newInstance();
			SAXParser sp = spf.newSAXParser();
			XMLReader xr = sp.getXMLReader();

			xr.setContentHandler(mHandler);
			xr.parse(retrieveInputStream(responseEntity));

		} catch (ClientProtocolException e) {
			client.getConnectionManager().shutdown();
			e.printStackTrace();
		} catch (IOException e) {
			client.getConnectionManager().shutdown();
			e.printStackTrace();
		} catch (Exception e) {
			Log.e(TAG, "executeRequest(): " + e.toString());
			e.printStackTrace();
		}
	}

	private InputSource retrieveInputStream(HttpEntity responseEntity) {
		InputSource insrc = null;
		try {
			insrc = new InputSource(responseEntity.getContent());
		} catch (Exception e) {
			Log.e(TAG, "retrieveInputStream(): " + e.toString());
			e.printStackTrace();
		}
		return insrc;
	}
}
